<?php
/**
 * Notes API
 * 
 * @author Xavier VILAIN
 * @package Hive\Ressource\Note
 * 
 */

use Hive\Ressource\Note;
$xview = Xview::instance();
$id = $xview->val('rid');
$notid = $xview->val('notid');
$noteContent = $xview->val('notes');

$contact = $xview->component('ressource','object');

if(empty($notid)) {
	$note = $contact->note();
} else {
	try {
		$note = Fox\Dao::load(Note::className(),$id.':'.$notid);
	} catch(Fox\Object\Exception\NotExist $e) {
		$note = $contact->note();
	}
}

try {
	$data['note'] = Fox\Utilities::UTF8Decode($noteContent);
	$data['author'] = User::current()->getId();
	if($note->exists()) {
		$note->setFields($data);
		$note->update();
	} else {
		$data['type'] = Note::TYPE_DEFAULT;
		$data['ressource_id'] = $contact->getId();
		$note->setFields($data);
		$note->create();
	}
	Fox\Message::Success(msg(Fox\Msg::BUNDLE,Fox\Msg::UPDATE_DONE));
} catch(Fox\Object\Exception\Save $e) {
	Fox\Message::Error($e);
}
echo $xview->messages(true,true);